package com.kpf.springcloud.config;

import com.aliyun.oss.OSSClient;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;

public class OSSClientUtils {

    private static String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    private static String accessKeyId = "XXXXXXXXXXXXX【自行替换】XXXXXXXXXXXXXXXX";
    private static String accessKeySecret = "XXXXXXXXXXXXX【自行替换】XXXXXXXXXXXXXXXX";

    public static String putAppImg(String name, byte[] data) {
        String bucket = "xinli001-image";
        OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        InputStream is = new ByteArrayInputStream(data);
        client.putObject(bucket, name, is);
        client.shutdown();
        System.out.println();
        return "https:///ossimg.xinli001.com/" + name;
    }

    public static String putFile(String name, byte[] data) {
        OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        String bucket = "xinli001-image";
        InputStream is = new ByteArrayInputStream(data);
        client.putObject(bucket, name, is);
        client.shutdown();
        return "https://ossimg.xinli001.com/" + name;
    }

    public static String putAppImg(String name, File file) {
        OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        String bucket = "xinli001-image";
        client.putObject(bucket, name, file);
        client.shutdown();
        return "https://ossimg.xinli001.com/" + name;
    }

}